var commonObj = {
	'init': function() {
		var pageObj = new pageFun();
		/*参数每页多少个,当前页,总个数,容器的ID*/
		pageObj.init(5, 1, 50, 'itemContainer');

		/*		初始化复选框*/
		var checkAllobj = $("thead .checkbox input:checkbox");
		var checklistObj = $("tbody .checkbox input:checkbox");
		checkBoxFun(checkAllobj, checklistObj);
		selectObj.showChangeValue();

	},
	'checkinit': function() {
		/*初始化复选框*/
		var checkAllobj = $("thead .checkbox input:checkbox");
		var checklistObj = $("tbody .checkbox input:checkbox");
		checkBoxFun(checkAllobj, checklistObj);
		selectObj.showChangeValue();
	},
	'selectfile': function(obj) {
		var name = document.getElementById("filename");
		var filePath = obj.value;
		if(filePath.indexOf("jpg") != -1 || filePath.indexOf("png") != -1) {
			var arr = filePath.split('\\');
			var fileName = arr[arr.length - 1];
			name.innerHTML = fileName;
		} else {
			$('.shangchuan').hide();
			name.innerHTML = "您未上传文件，或者您上传文件类型有误！";
		}

	},
	'srcoll': function(objId, height, width) {
		width = width ? width : '100%',
				$('#' + objId).slimScroll({
					color: 'rgba(0,0,0,0.8)',
					size: '5px',
					height: height,
					width: width,
					alwaysVisible: true,
					borderRadius: '0px', //滚动条圆角
				});
	},

}

/*控制checkbox全选的*/
/*控制checkbox全选的*/
function checkBoxFun(checkAllobj, checklistObj) {
	$(checkAllobj).click(function() {
		var checkStatus = $(this)[0].checked;
		if(checkStatus) {
			$(this).parent().addClass('checked_checkbox');
		} else {
			$(this).parent().removeClass('checked_checkbox');
		}
		var self = this;
		$(checklistObj).each(function() {
			if(checkStatus) {
				//有计划单不能选中
				if($(this).attr("readonly") != "readonly") {
					$(this).parent().addClass('checked_checkbox');
					$(this)[0].checked = true;
				}
			} else {
				//没有计划单可以选中
				if($(this).attr("readonly") != "readonly") {
					$(this).parent().removeClass('checked_checkbox');
					$(this)[0].checked = false;
				}
			}
		})
	})

	$(checklistObj).each(function() {
		$(this).click(function() {
			var checkStatus = $(this)[0].checked;
			if(checkStatus) {
				//有计划单不能选中
				if($(this).attr("readonly") != "readonly") {
					$(this).parent().addClass('checked_checkbox');
					$(this)[0].checked = true;
				} else {
					$(this)[0].checked = false;
				}
			} else {
				//没有计划单可以选中
				if($(this).attr("readonly") != "readonly") {
					$(this).parent().removeClass('checked_checkbox');
					$(this)[0].checked = false;
				}
			}
		})
	})
}

function stopPropagation(e) {
	if(e.stopPropagation)
		e.stopPropagation();
	else
		e.cancelBubble = true;
}

/*这个是屏蔽掉ul仿照select事件的时候，点击其他对象的时候影藏掉ul*/

var _FirstClickEle, _lastClickEle, clickCount = 0;
function bindClickFun() {
	$('.overlay').each(function(){
		var obj=$(this);
		var liObj=$(obj).find(' ul li');
		var len=$(liObj).length*29;
		var $par=$(obj).find('ul').parent();
		if(len>145){
			$(obj).find('ul').slimScroll({
				color: 'rgba(0,0,0,0.8)',
				size: '5px',
				height: '145px',
				width: '100%',
				alwaysVisible: true,
				borderRadius: '0px', //滚动条圆角
			});
		}else{
			if($par.hasClass('slimScrollDiv')){
				$par.replaceWith($(obj).find('ul')); 
			}
			
		}
	})
	
	
}
$(document).bind('click', function(e) {
    $('.dropdown').removeClass('scrollHidden');
	$('.dropdown').find('ul').addClass('none');
});

/**
 * 这个是select样式覆盖之后的操作
 */
var selectObj = {
	/*修改城市*/
	showChangeValue: function(callback) {
		bindClickFun();
		var callbackFun = (typeof(callback) == 'function') ? callback : '';
	 	this.dropdown();
	},
	changeEvent: function() {
		bindClickFun();
		var callbackFun = (typeof(callback) == 'function') ? callback : '';
		var a = false;
		this.dropdown();
	},
	dropdown:function(){
		this.initData();
		$('.dropdown-toggle').click(function(e) {
				var e = e || window.event; /*浏览器兼容性*/
				var $p=	$(this).parent();
				
  				console.log($p.hasClass('scrollHidden'));
  				if($p.hasClass('scrollHidden')){
  					$p.removeClass('scrollHidden');
					$p.find('ul').addClass('none');
  				}else{
  					$p.addClass('scrollHidden');
					$p.find('ul').removeClass('none');
  				}
  			 
				$('.dropdown').not($p).find('ul').addClass('none');
				$('.dropdown').not($p).removeClass('scrollHidden');
				e.stopPropagation();
			});	
			 this.liClicked();
	},
	initData:function(){
		//初始化下拉框显示的值
		$('.dropdown-toggle').each(function(){
			var defaultValue = $(this).parent().prev().val();
			var currentElements = $(this);
			if(defaultValue != undefined && defaultValue != "") {
				$.each($(this).next().find("li"), function(i, v) {
						if($(v).attr("data-value") == defaultValue) {
							$(currentElements).find(".value").text($(v).text());
						}
				})
			}
		})
		
	},
	liClicked:function(){
		$('.dropdown').find('ul').find('li').click(function(e) {
			var $ulparent=$(this).parent();
			var parent=$(this).parent().parent();			
			if(parent.hasClass('slimScrollDiv')){
				var $p=$(this).parent().parent().parent();
			}else{
			   var $p=$(this).parent().parent();
			}	
			$(this).siblings().removeClass('selectedLi');
			$(this).addClass('selectedLi');

			$p.find('.value').html($(this).html());
			$ulparent.addClass('none');
			$p.removeClass('scrollHidden');
			$p.prev().val($(this).attr('data-value'));
		})
	},
}
/*这里是操作产品用到的一些JS*/
var productObj = {
	staticPath: "",
	serverPath: "",
	oldProductName: "",
	product: {
		id: "",
		productName: "",
		picUrl: "",
		province: "",
		city: "",
		categoryId: "",
		productDetail: "",
		displayImgUrl: ""
	},
	productInit: function() {
		selectObj.showChangeValue();

		/*调用省市区*/
		$.cxSelect.defaults.url = productObj.staticPath;
		$.cxSelect.defaults.emptyStyle = 'none';
		var data = $('.select-city').cxSelect({
			url: productObj.staticPath,
			/* 如果服务器不支持 .json 类型文件，请将文件改为 .js 文件*/
			selects: ['prov', 'city'],
			/*数组，请注意顺序*/
			emptyStyle: '',
			'htmlTagName': 'li',
			required: 'false',
		});
	},

	getProData: function() {
		var name = $('.pro_name').val();
		var pro_provincevalue = $('.pro_provincevalue').html();
		var pro_cityvalue = $('.pro_cityvalue').html();
		var pro_catevalue = $("#productCategoryId").val();
		var pro_desc = $('.pro_desc').val();
		var pro_img = $("input[name='picUrl']").val();
		var pro_displayImgUrl = $(".pro_img").attr("src");

		productObj.product.productName = name;
		productObj.product.picUrl = pro_img;
		productObj.product.province = pro_provincevalue;
		productObj.product.city = pro_cityvalue;
		productObj.product.categoryId = pro_catevalue;
		productObj.product.productDetail = pro_desc;
		productObj.product.displayImgUrl = pro_displayImgUrl;

		var index = parent.layer.getFrameIndex(window.name); /*获取窗口索引*/
		/*这里是提交产品的一些操作*/
		/*这个是产品的ID*/
		var id = $('.productId').val();
		if(id) {
			productObj.product.id = id;
			/*这里是编辑*/
			$.ajax({
				url: productObj.serverPath + "/qrcode/products/update",
				method: "get",
				async: false,
				dataType: "json",
				data: productObj.product,
				success: function(data) {
					parent.$('.editPro').each(function() {
						var _self = $(this).parent().parent().parent();
						console.log(_self);
						if($(this).attr('data-id') == id) {
							$(_self).find('.lazy').attr('src', productObj.product.displayImgUrl);
							$(_self).find('.pro_name').html(productObj.product.productName);
							var val = $("#productCategoryId").next().find('.value').html();
							$(_self).find('.categoryName').html(val);
						}
					})
					parent.layer.close(index);
				},
				error: function() {
					console.log('保存出错');
				}
			})
		} else {
			/*这里是新增*/
			$.ajax({
				url: productObj.serverPath + "/qrcode/products/save",
				method: "get",
				async: false,
				dataType: "json",
				data: productObj.product,
				success: function(data) {
					parent.window.location.href = productObj.serverPath + "/qrcode/products/list";
				},
				error: function() {
					console.log('保存出错');
				}
			})
		}

	},
	checkProInfo: function() {
		//验证产品名是否重复
		$.validator.addMethod("isrepeat", function(value, element) {
			var productName = $("input[name='productName']").val();
			if(productName == productObj.oldProductName) {
				return true;
			}
			var isExists = false;
			$.ajax({
				url: productObj.serverPath + "/qrcode/products//validateProductName",
				async: false,
				data: {
					productName: productName
				},
				success: function(data) {
					isExists = data;
				},
				error: function() {
					//发生错误时，使用true，不进行保存
					isExists = true;
				}
			});
			if(isExists) {
				return false;
			}
			return true;
		}, "<i class=\" icon icon_error_red \"></i>产品名称已存在！");

		//这里是验证省市
		$.validator.addMethod("isareaEmpty", function(value, element) {
			if($("input[name='province']").val() && $("input[name='city']").val()) {
				return true;
			} else {
				return false;
			}
		}, "<i class=\" icon icon_error_red \"></i>请选择产品产地！");

		//这里是验证产品类型
		$.validator.addMethod("iscateEmpty", function(value, element) {
			if($('#productCategoryId').val()) {
				return true;
			} else {
				return false;
			}
		}, "<i class=\" icon icon_error_red \"></i>请选择产品分类！");

		//这里是验证图片是否上传
		$.validator.addMethod("ispicEmpty", function(value, element) {
			if($('#picUrl').val()) {
				return true;
			} else {
				return false;
			}
		}, "<i class=\" icon icon_error_red \"></i>请上传产品图片！");

		$("#commentForm").validate({
			rules: {
				productName: {
					required: true,
					maxlength: 16,
					isrepeat: true,
				},
				picname: {
					ispicEmpty: true,
				},
				proarea: {
					isareaEmpty: true,
				},
				procate: {
					iscateEmpty: true,
				},
				prodesc: {
					maxlength: 200,
				},
			},
			messages: {
				productName: {
					required: "<i class=\" icon icon_error_red \"></i>请输入产品名称！",
					maxlength: '<i class=\" icon icon_error_red \"></i>产品名称不得超过16个字'
				},
				prodesc: {
					maxlength: "<i class=\" icon icon_error_red \"></i>产品介绍不得超过200个字",
				},
			},
			errorPlacement: function(error, element) {
				error.appendTo('<i class=\" icon icon_error_red \"></i>').appendTo(element.parent());
			},
		});
	},
	formData: function(layero, obj) {}
} /*搜索框中的页面重置功能*/
function resetForm() {
	$('.form').find("input[type='text']").val("");
	$('.form').find("input[type='hidden']").val("");
	$('.form').find(".dropdown-toggle").find(".value").each(function() {
		$(this).html($(this).attr('data-default'));
	});
}